package com.glory.huaweipda.viewmodel

import android.Manifest
import android.app.Activity
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ObservableArrayList
import androidx.lifecycle.MutableLiveData
import com.glory.huaweipda.BR
import com.glory.huaweipda.R
import com.glory.huaweipda.base.Constant
import com.glory.huaweipda.base.MyApp
import com.glory.huaweipda.entity.LoginEntity
import com.glory.huaweipda.model.TaskRepository
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.CallObserveBean
import com.glorysoft.hlgdpda.utils.MyUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils
import me.tatarka.bindingcollectionadapter2.ItemBinding
import pub.devrel.easypermissions.EasyPermissions
import java.io.InputStream
import java.util.*

/**
 * FileName: MainViewModel
 * Author: XJH
 * Date: 2021/5/13 17:36
 * Description:
 * History:
 */
class HomeViewModel : BaseViewModel() {

    var mfgItems = ObservableArrayList<FreeUI_TabIndexGridEntity>()
    val START_ACTIVITY = 0

    var loginEntity = MutableLiveData<LoginEntity>()
    private val repository by lazy { TaskRepository() }

    private val mainItemClick = object : OnItemClickListener {
        override fun onItemClick(item: Any) {

            defUI.callObserve.value = CallObserveBean(START_ACTIVITY, item)
        }
    }

    var itemBinding = ItemBinding.of<FreeUI_TabIndexGridEntity>(BR.tabIndexGrid, R.layout.item_main)
            .bindExtra(BR.itemClickListener, mainItemClick)

    fun initAssets(context : Activity){
        val mfgJsonStream: InputStream = context.resources.assets.open("mfg.txt")
        val mfgJson = FreeApi_StringUtils.getString(mfgJsonStream)
        val mfgEntity :List<FreeUI_TabIndexGridEntity> = Gson().fromJson(mfgJson, object : TypeToken<List<FreeUI_TabIndexGridEntity?>?>() {}.type)
        mfgItems.addAll(MyUtils.setPicToDrawable(context,mfgEntity))

    }

    fun initUser(context: AppCompatActivity){
        loginEntity.value = LoginEntity()
        Constant.REQUEST_URL_DEBUG = FreeApi_SharePreferencesUtils.getSharePre("SystemSet", "REQUEST_URL_DEBUG", Constant.REQUEST_URL_DEBUG, MyApp.app)
        requestPermission(context)
        detectionUpdate()
    }

    private fun detectionUpdate() {
        val map: Map<String, String> = HashMap()
        launchOnlyResult({
            val defaultUpdate = repository.defaultUpdate(
                map)
            defUI.callObserve.value = CallObserveBean(2, defaultUpdate)
        })
    }

    private fun requestPermission(context: Activity){
        //Manifest.permission.READ_PHONE_STATE 申请不到
        val perms = arrayOf(
            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
        //权限判断，第一次弹出系统的授权提示框
        if (EasyPermissions.hasPermissions(context, *perms)) {
            //@AfterPermissionGranted 有权限直接执行 todo...
            //login();
        } else {
            //没有权限的话，先提示，点确定后弹出系统的授权提示框
            EasyPermissions.requestPermissions(context, "程序运行需要权限",
                10, *perms)
        }
    }

    interface OnItemClickListener {
        fun onItemClick(item: Any)
    }
}